/**
 * 
 */
package cz.geoserver.KMLpublisher.db.model;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 * Class for modeling database table clusters_info with additional info about clusters
 * @author mkepka
 *
 */
public class ClusterInfo {

    private int id;
    private String dataTableName;
    private String dataAttributeName;
    private String dataIdColumnName;
    private String clustersTableName;
    private int levelsCount;
    private String maxBBOXGeom;
    private double maxBBOXArea;
    //private Map<Integer, Integer> pointsAtLevel;
    private String clusterFeatureIdName;
    private String clusterLevelName;

    /**
     * Constructor creates instance of ClusterInfo class from database ResultSet object
     * @param res - ResultSet object that contains a row from database table clusters_info
     * @throws SQLException - Throws SQLException if an exception occurs while getting a value form ResultSet object
     */
    public ClusterInfo(ResultSet res) throws SQLException{
        this.id=res.getInt("id");
        this.dataTableName=res.getString("data_table");
        this.dataAttributeName=res.getString("data_attribute");
        this.dataIdColumnName= res.getString("data_id");
        this.clustersTableName=res.getString("clusters_table");
        this.clusterFeatureIdName=res.getString("clusters_feature_id");
        this.clusterLevelName=res.getString("clusters_level_column");
        this.levelsCount=res.getInt("levels_count");
        this.maxBBOXGeom=res.getString("max_bbox");
        this.maxBBOXArea=res.getDouble("max_bbox_area");
        //this.pointsAtLevel = readPointAtLevel(res.getString("points_at_level"));
    }
    
    /**
     * Constructor for creating class during JUnit tests
     * @param id - Id of current row - combination of data table, data attribute
     * @param dataTabName - name of table with geometry
     * @param dataAttName - name of column that was used in clusterization
     * @param dataIdName - name of column that contain geometry identifier, mostly primary key of table 
     * @param clusterTabName - name of table with hierarchy of geometries
     * @param clustersFeatId - name of column with feature identifier in cluster table
     * @param clusterLevelCol - name of column with number of current level in cluster table
     * @param levels - number of level in hierarchy
     * @param maxBoxGeom - geometry of maximal BBOX
     * @param maxBoxArea - area of maximal BBOX in square meters
     */
    public ClusterInfo(int id, String dataTabName, String dataAttName, String dataIdName, String clusterTabName, String clustersFeatId, String clusterLevelCol, int levels, String maxBoxGeom, double maxBoxArea){
        this.id = id;
        this.dataTableName = dataTabName;
        this.dataAttributeName = dataAttName;
        this.dataIdColumnName = dataIdName;
        this.clustersTableName = clusterTabName;
        this.clusterFeatureIdName = clustersFeatId;
        this.clusterLevelName = clusterLevelCol;
        this.levelsCount = levels;
        this.maxBBOXGeom = maxBoxGeom;
        this.maxBBOXArea = maxBoxArea;
        //this.pointsAtLevel = featAtLevel;
    }

    /**
     * Getter returns Id of current row in database table
     * @return the id - Id of current row in database table
     */
    public int getId() {
        return id;
    }

    /**
     * Getter returns name of table with geometry
     * @return the dataTableName - name of table with geometry
     */
    public String getDataTableName() {
        return dataTableName;
    }

    /**
     * Getter returns name of column that was used in clusterization
     * @return the dataAttributeName - name of column that was used in clusterization
     */
    public String getDataAttributeName() {
        return dataAttributeName;
    }

    /**
     * Getter returns name of column that contain geometry identifier, mostly primary key of table
     * @return the geomIdColumnName - name of column that contain geometry identifier, mostly primary key of table
     */
    public String getDataIdColumnName() {
        return dataIdColumnName;
    }

    /**
     * Getter returns name of table with hierarchy of geometries
     * @return the clustersTableName - name of table with hierarchy of geometries
     */
    public String getClustersTableName() {
        return clustersTableName;
    }

    /**
     * Getter returns name of column with feature identifier in cluster table 
     * @return the clusterFeatureIdName - name of column with feature identifier in cluster table
     */
    public String getClusterFeatureIdName() {
        return clusterFeatureIdName;
    }

    /**
     * Getter returns name of column with number of current level in cluster table
     * @return the clusterLevelName - name of column with number of current level in cluster table
     */
    public String getClusterLevelName() {
        return clusterLevelName;
    }

    /**
     * Getter returns number of level in hierarchy
     * @return the levelsCount - number of level in hierarchy
     */
    public int getLevelsCount() {
        return levelsCount;
    }

    /**
     * Getter returns geometry of maximal BBOX
     * @return the maxBBOXGeom - geometry of maximal BBOX
     */
    public String getMaxBBOXGeom() {
        return maxBBOXGeom;
    }

    /**
     * Getter returns area of maximal BBOX in square meters
     * @return the maxBBOXArea - area of maximal BBOX in square meters
     */
    public double getMaxBBOXArea() {
        return maxBBOXArea;
    }

    /**
     * Methods for decoding String with number of features in cluster levels, 
     * created by procedure in database in form of "[0=number;1=number;2=number;...]"
     * This method is not used at this time
     * @param pointAtLevelString String with features at levels 
     * @return Map with pairs (level, number of points)
     */
    private Map<Integer, Integer> readPointAtLevel(String pointAtLevelString){
        Map<Integer, Integer> pointsAtLevel = new HashMap<Integer,Integer>();
        pointAtLevelString=pointAtLevelString.replace("[", "");
        pointAtLevelString=pointAtLevelString.replace("]", "");
        String[] levels = pointAtLevelString.split(";");
        for(int i = 0; i<levels.length;i++){
            String[] level = levels[i].split("=");
            pointsAtLevel.put(Integer.valueOf(level[0]), Integer.valueOf(level[1]));
        }
        return pointsAtLevel;
    }
}